package com.woniuxy.automotivemanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.automotivemanagementsystem.entity.News;

import com.woniuxy.automotivemanagementsystem.entity.vo.NewsVo;
import com.woniuxy.automotivemanagementsystem.mapper.NewsMapper;
import com.woniuxy.automotivemanagementsystem.service.NewsService;
import com.woniuxy.automotivemanagementsystem.utils.PageResult;
import com.woniuxy.automotivemanagementsystem.utils.Result;
import org.springframework.stereotype.Service;

@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    /*
    * 条件查询所有
    * */
    @Override
    public Result queryCondition(NewsVo vo) {
        LambdaQueryWrapper<News> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(vo.getTitle()!=null && !vo.getTitle().trim().isEmpty(),News::getTitle,vo.getTitle())
                .eq(vo.getType()!=null && !vo.getType().trim().isEmpty(),News::getType,vo.getType())
                .eq(vo.getStatus()!=null,News::getStatus,vo.getStatus())
                .le(vo.getCreatetime()!=null,News::getCreatetime,vo.getCreatetime());
        queryWrapper.orderByDesc(News::getCreatetime);//时间降序
        //2.分页查询
        Page<News> page = this.page(new Page<>(vo.getPageNum(),vo.getPageSize()), queryWrapper);
        //3.封装响应数据
        PageResult<News> pageResult = new PageResult<>(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());
        return Result.success(pageResult); //res.data.data.list
    }
}
